package br.com.iplant.mb;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.com.iplant.dao.SafraDAO;
import br.com.iplant.util.Conexao;
import br.com.iplant.vo.Safra;


public class CadastroSafra extends JPanel implements ActionListener {
	JLabel l1, l2, l3, l4;
	JButton b1, b2, b3, b4, b5;
	static JTextField tfSafraId, tfAno, tfSequencia;
	JPanel p1 = new JPanel();
	JPanel p2 = new JPanel();
	Conexao con = new Conexao();
	Safra safra = new Safra();
	SafraDAO safraDAO = new SafraDAO();

	public CadastroSafra() {
		
		setSize(600, 400);
		
		p1.setLayout(null);
		p1.setSize(600, 100);
		p2.setSize(600, 40);
		l2 = new JLabel("Ano:");
		l3 = new JLabel("Sequencia:");
		l4 = new JLabel(" ");

	
		tfAno = new JTextField(15);
		tfSequencia = new JTextField(15);
		b1 = new JButton("Cadastrar");
		b1.addActionListener(this);
		b2 = new JButton("Atualizar");
		b2.addActionListener(this);
		b3 = new JButton("Excluir");
		b3.addActionListener(this);
		b4 = new JButton("Localizar");
		b4.addActionListener(this);
		b5 = new JButton("Novo");
		b5.addActionListener(this);
		//l1.setBounds(10, 10, 60, 25);
		//tfSafraId.setBounds(70, 10, 60, 25);

		l2.setBounds(10, 50, 70, 25);
		tfAno.setBounds(70, 50, 130, 25);

		l3.setBounds(10, 90, 70, 25);
		tfSequencia.setBounds(70, 90, 130, 25);

		l4.setBounds(230, 10, 80, 25);

	
		p1.add(l2);
		p1.add(tfAno);
		p1.add(l3);
		p1.add(tfSequencia);
		p1.add(l4);
		p2.add(b1);
		p2.add(b2);
		p2.add(b3);
		p2.add(b4);
		p2.add(b5);
		setLayout(new BorderLayout());
		add(p1);
		add(p2, "South");
	}

	public static void main(String[] args) {
		new CadastroSafra().setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b5) {
			limpaCampos();
			return;
		}
		if (e.getSource() == b1) {
			safra.setAno(Integer.parseInt(tfAno.getText()));
			safra.setSequencia(Integer.parseInt(tfSequencia.getText()));

			safraDAO.salvar(safra);
			// limpaCampos();
		}

		if (e.getSource() == b4) {

			safra = safraDAO.buscarUM(new Safra(Integer.parseInt(tfSafraId
					.getText())));
			tfAno.setText(String.valueOf(safra.getAno()));
			tfSequencia.setText(String.valueOf(safra.getSequencia()));
		}

	}

	public static void limpaCampos() {
	
		tfAno.setText("");
		tfSequencia.setText("");
	}

	public Safra getSafra() {
		return safra;
	}

	public void setSafra(Safra safra) {
		this.safra = safra;
	}

	public void addSafra(Safra safra) {
		
		tfAno.setText(String.valueOf(safra.getAno()));
		tfSequencia.setText(String.valueOf(safra.getSequencia()));
	}
}
